<?php
namespace app\daohang\event;

use app\daohang\event\Info;

class Mp extends Info
{
    public function _initialize()
    {
        parent::_initialize();
    }
    
    public function create()
    {
        $this->assign('query', $this->query);
        
        $this->assign('items', model('daohang/Mp','loglic')->items($this->query));
        
        return $this->fetch('daohang@mp/create');
    }
    
    public function edit()
    {
        $info_id = input('id/d',0);
        if(!$info_id){
            $this->error(lang('mustIn'));
        }
        //查询数据
        $data = daohangGet(['cache'=>false,'id'=>$info_id]);
        if( is_null($data) ){
            $this->error(lang('empty'));
        }
        //定义变量
        $this->assign('data', $data);
        $this->assign('items', model('daohang/Mp','loglic')->items($data));
        return $this->fetch('daohang@mp/edit');
    }
    
    public function index()
    {
        if($this->request->isAjax()){
            $args = array();
            $args['cache']    = false;
            $args['search']   = $this->query['searchText'];
            $args['limit']    = DcEmpty($this->query['pageSize'], 50);
            $args['page']     = DcEmpty($this->query['pageNumber'], 1);
            $args['sort']     = DcEmpty($this->query['sortName'], 'info_id');
            $args['order']    = DcEmpty($this->query['sortOrder'], 'desc');
            $args['module']   = DcEmpty($this->query['info_module'], 'daohang');
            $args['controll'] = DcEmpty($this->query['info_controll'], 'mp');
            $args['action']   = $this->query['info_action'];
            $args['type']     = $this->query['info_type'];
            $args['status']   = $this->query['info_status'];
            //自定义字段排序
            if( !in_array($args['sort'],['info_id','info_order','info_views','info_hits','info_create_time','info_update_time']) ){
                $args['meta_key'] = $args['sort'];
                $args['sort']     = 'meta_value_num';
            }
            //按分类ID筛选
            if($this->query['category_id']){
                $args['term_id'] = ['in',DcTermSubIds($this->query['category_id'],'category','array')];
            }
            //查询数据
            $list = daohangSelect( DcArrayEmpty($args) );
            if( is_null($list) ){
                return json(['total'=>0,'data'=>[]]);
            }
            foreach($list['data'] as $key=>$value){
                $list['data'][$key]['info_type_text'] = lang('dh_type_'.$value['info_type']);
                $list['data'][$key]['category_names'] = implode(',',$value['category_name']);
            }
            return json($list);
		}
        //地址栏参数
        $this->assign('query', $this->query);
        //表单筛选字段
        $this->assign('items', model('daohang/Mp','loglic')->items($this->query));
        //表格列字段
        $this->assign('columns', model('daohang/Mp','loglic')->columns($this->query));
        //加载模板
        return $this->fetch('daohang@mp/index');
	}
}